﻿using DevExpress.XtraEditors;
using DLL;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class SetEduProject : Form
    {
        public string Guid = string.Empty;
        public int count = 0;
        public SetEduProject(string classguid)
        {
            InitializeComponent();
            Guid = classguid;
            select();
            Text = "项目管理";
        }

        private void select()
        {
            string sql = @"SELECT * FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject](NOLOCK) where ClassGuid = '" + Guid + "' order by OrderNum";
            DataTable Class = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
            count = Class.Rows.Count;
            labelControl1.Text = "此表单共用项目数目：";
            labelControl1.Text += count.ToString();
            for (int i = 0; i < count; i++)
            {
                DevExpress.XtraEditors.CheckEdit chkb = new CheckEdit();
                chkb.Name = string.Format(i.ToString());
                chkb.Text = string.Format(Class.Rows[i]["ProjectName"].ToString());
                chkb.Tag = string.Format(Class.Rows[i]["ProjectGuid"].ToString());
                chkb.Padding = new Padding(0, 0, 5, 0);
                chkb.AutoSize = true;
                chkb.Width = 500;
                chkb.Location = new Point(20, 60 + 20 * i);
                Controls.Add(chkb);
            }
            Height = 150 + 20 * count;
            if (0 == count)
            {
                simpleButton2.Hide();
                simpleButton3.Hide();
                simpleButton4.Hide();
                labelControl1.Text = "请添加项目";
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddEduProject F1 = new AddEduProject("-1", Guid);
            F1.ShowDialog();
            if (F1.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Hide();
                SetEduProject F5 = new SetEduProject(Guid);
                F5.ShowDialog();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                    if (check > 1)
                    {
                        XtraMessageBox.Show("仅支持一次修改一条项目!");
                        return;
                    }
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要修改的项目!");
                return;
            }
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    string classguid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                    AddEduProject F1 = new AddEduProject(classguid, Guid);
                    F1.ShowDialog();
                    if (F1.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        Hide();
                        SetEduProject F5 = new SetEduProject(Guid);
                        F5.ShowDialog();
                    }
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要删除的项目!");
                return;
            }
            if (XtraMessageBox.Show("你确定要删除选中的项目吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                for (int i = 0; i < count; i++)
                {
                    if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                    {
                        string projectid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                        string sql = @" delete from [BNNursingDocsSystem_Develop].[dbo].[HealthEduProject] where ProjectGuid ='" + projectid + "'";
                        SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                        sql = @"delete  FROM [BNNursingDocsSystem_Develop].[dbo].[HealthEduContent] where ProjectGuid ='" + projectid + "'";
                        SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                    }
                }
                Hide();
                SetEduProject F5 = new SetEduProject(Guid);
                F5.ShowDialog();
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            int check = 0;
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    check++;
                    if (check > 1)
                    {
                        XtraMessageBox.Show("仅支持一次管理一条项目中的内容!");
                        return;
                    }
                }
            }
            if (check == 0)
            {
                XtraMessageBox.Show("请选择要管理的分类内容!");
                return;
            }
            for (int i = 0; i < count; i++)
            {
                if (((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Checked)
                {
                    string projectguid = ((DevExpress.XtraEditors.CheckEdit)(Controls[i.ToString()])).Tag.ToString();
                    ChangeEduContent F1 = new ChangeEduContent(projectguid);
                    F1.ShowDialog();
                    if (F1.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        Hide();
                        SetEduProject F5 = new SetEduProject(Guid);
                        F5.ShowDialog();
                    }
                }
            }
        }
    }
}
